/*
 * Copyright (c) 2010, artur
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the author nor the names of any co-contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

/*
 * kiddie-os
 * Created on: 14.04.2010
 *     Author: artur
 *
 */

#ifndef _KIDDIE_H_
#define _KIDDIE_H_

#ifndef AUTOCONF_INCLUDED
#include <kiddie/autoconf.h>
#endif

#include <atoms.h>
#include <platform.h>

#include <kiddie/command.h>

#define EXIT	do {} while (0)
#define ENTER	do {} while (0)

#define OUTDEBUG(str)  ::cout << (char *)""str" " \
    << (char *)__FILE__ << "::" \
    << (char *)__FUNCTION__ << "\n"
//\
//									<< "():" << (int)__LINE__ << "\n"

#define BUG() 	do { volatile int *q = (int *)0x10;\
    *q = 0x100; } while(0)

namespace util {

	void *memset(void *s, char c, size_t n);
	void *memsetw(void *s, const unsigned short w, size_t n);
	void *memcpy(void *dest, const unsigned char *src, size_t n);
	int strncmp(const char *s, const char *t, size_t n);
	int strcmp (const char *s, const char *t);

	class Capabilities;
	class IRunnable;
}

namespace math {

}

/**
 * Namespace definitions
 */
namespace kernel {

	namespace sys
	{
		namespace sched {
			namespace task {
				class Task;
				class TaskFactory;
			}
		}
	}

	class Console;
	//
	// void initcalls(void);
	void initcalls(void *);
//	void puts(const char *s);
//	void putchar(const char c);

	/**
	 *
	 */
	namespace irq {
//		void tableinit(void);
//		void disable(void);
//		void enable(void);
	}
	/**
	 *
	 */
	namespace trap {
	}

	/**
	 * Cpu namespace
	 */
	namespace cpu {
		class Cpu;
	}

	/**
	 *
	 */
	namespace mem {
		int init(void);

		class MemBlock;
		class VmMap;
		class MemFactory;

		namespace page {
			class PageFactory;
		}
	}

}

#ifdef __cplusplus
typedef util::IRunnable *(*module_call_t)(util::Capabilities &);
//#define imodule_init(fname,fn) \
//		static module_call_t __attribute__((used)) \
//		__attribute__ ((__section__(".modules.init"))) __modulecall_##fname = fn;
#define imodule_init(fname,fn) \
        static module_call_t __attribute__((used)) \
        __attribute__ ((__section__(".initcall.init"))) __modulecall_##fname = fn;
#endif


#define CHECK_FLAG(flags,bit)   ((flags) & (1 << (bit)))
#define POS_TO_BIT(pos)   (1 << (pos))

#ifdef __cplusplus
extern "C" {
#endif
	/**
	 * LDS sections
	 */
	/* these are symbols from ld-script*/
	extern unsigned long __page_offset;
	extern unsigned long __kernel_vstart;
	extern unsigned long __kernel_vpgd;
	extern unsigned long __ivec_pt_base;
	extern unsigned long __boot_info_p;
	extern unsigned long _end;
	/* init section symbols */
	extern unsigned long __initcall_start;
	extern unsigned long __early_initcall_end;
	extern unsigned long __initcall_end;
	extern unsigned long exception_vector_start;
	extern unsigned long exception_vector_end;


	reg_t __up_interrupt_handler (struct cpu_regs *regs);
	reg_t __up_abort_handler (struct cpu_regs *regs);
	reg_t __up_prefetch_handler (struct cpu_regs *regs);
	reg_t __up_undefined_handler (struct cpu_regs *regs);


#ifdef __cplusplus
};
#endif


/**
 * FIXME!!!!!!!!!!!!!1
 */
#define mybus_to_virt(pa) (void *)((unsigned long)pa + (unsigned long)V_IO_BASE - (unsigned long)P_IO_BASE)


#endif /* _KIDDIE_H_ */
